package com.haixiaoke.saas.contract.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.haixiaoke.saas.common.constant.ContractConstants;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.contract.domain.HouseCommission;
import com.haixiaoke.saas.contract.domain.Proprietor;
import com.haixiaoke.saas.contract.mapper.HouseCommissionMapper;
import com.haixiaoke.saas.contract.service.IHouseCommissionService;
import com.haixiaoke.saas.contract.service.IProprietorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 房屋委托信息Service业务层处理
 *
 * @author xiaolong
 * @date 2024-06-06
 */
@Service
public class HouseCommissionServiceImpl implements IHouseCommissionService {
    @Autowired
    private HouseCommissionMapper houseCommissionMapper;
    @Autowired
    private IProprietorService proprietorService;

    /**
     * 查询房屋委托信息
     *
     * @param houseCommissionId 房屋委托信息主键
     * @return 房屋委托信息
     */
    @Override
    public HouseCommission selectHouseCommissionByHouseCommissionId(Long houseCommissionId) {
        return houseCommissionMapper.selectHouseCommissionByHouseCommissionId(houseCommissionId);
    }

    /**
     * 查询房屋委托信息列表
     *
     * @param houseCommission 房屋委托信息
     * @return 房屋委托信息
     */
    @Override
    public List<HouseCommission> selectHouseCommissionList(HouseCommission houseCommission) {
        return houseCommissionMapper.selectHouseCommissionList(houseCommission);
    }

    /**
     * 新增房屋委托信息
     *
     * @param houseCommission 房屋委托信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHouseCommission(HouseCommission houseCommission) {
        Proprietor proprietor = proprietorService.selectProprietorByProprietorId(houseCommission.getProprietorId());
        if (ObjectUtil.isEmpty(proprietor)) {
            throw new ServiceException("业主信息不存在");
        }
        proprietor.setUpdateTime(DateUtils.getNowDate());
        proprietor.setUpdateBy(houseCommission.getCreateBy());
        proprietor.setHouseCommissionStatus(ContractConstants.HOUSE_COMMISSION_STATUS_FINISH);
        proprietorService.updateProprietor(proprietor);
        houseCommission.setCreateTime(DateUtils.getNowDate());

        return houseCommissionMapper.insertHouseCommission(houseCommission);
    }

    /**
     * 修改房屋委托信息
     *
     * @param houseCommission 房屋委托信息
     * @return 结果
     */
    @Override
    public int updateHouseCommission(HouseCommission houseCommission) {
        houseCommission.setUpdateTime(DateUtils.getNowDate());
        return houseCommissionMapper.updateHouseCommission(houseCommission);
    }

    /**
     * 批量删除房屋委托信息
     *
     * @param houseCommissionIds 需要删除的房屋委托信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseCommissionByHouseCommissionIds(Long[] houseCommissionIds) {
        return houseCommissionMapper.deleteHouseCommissionByHouseCommissionIds(houseCommissionIds);
    }

    /**
     * 删除房屋委托信息信息
     *
     * @param houseCommissionId 房屋委托信息主键
     * @return 结果
     */
    @Override
    public int deleteHouseCommissionByHouseCommissionId(Long houseCommissionId) {
        return houseCommissionMapper.deleteHouseCommissionByHouseCommissionId(houseCommissionId);
    }

    @Override
    public HouseCommission selectHouseCommissionByProprietorId(Long proprietorId) {
        return houseCommissionMapper.selectHouseCommissionByProprietorId(proprietorId);
    }
}
